﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Windows;
using PhoneAppRssReader.Entity;

namespace PhoneAppRssReader
{
    public partial class FeedDataPage : INotifyPropertyChanged
    {
        private Feed _feed;

        public static List<FeedItem> FeedData { get; set; }

        public FeedDataPage()
        {
            InitializeComponent();
            mainPage.PropertyChanged += (sender, e) =>
                                            {
                                                if (e.PropertyName.Equals("Request"))
                                                {
                                                    //mainPage.FeedData.IsDataLoaded = false;
                                                    mainPage.FeedData.GetFeedWebClient(_feed);
                                                    mainPage.IsProgressBarVisiable = true;
                                                }
                                                else if (e.PropertyName.Equals("ActiveIndexItem"))
                                                {
                                                    //selected item rss
                                                    //navigate page
                                                    FeedData = mainPage.FeedItems;
                                                    NavigationService.Navigate(new Uri("/DetailFeedItem.xaml?index=" + mainPage.ActiveIndexItem, UriKind.RelativeOrAbsolute));


                                                }
                                                else if (e.PropertyName.Equals("Error"))
                                                {
                                                    MessageBox.Show("Error!!! Please try again next time...");
                                                    NavigationService.Navigate(new Uri("/MainPage.xaml",
                                                                                       UriKind.RelativeOrAbsolute));
                                                }

                                            };

        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                var feedId = Convert.ToInt16(NavigationContext.QueryString["id"]);
                _feed = App.Source.Items.First(i => i.Id == feedId);
                if (_feed == null) return;
                if (_feed.Update.Year == 1)
                    _feed.Update = DateTime.Now;
                if (!mainPage.FeedData.IsDataLoaded)
                    mainPage.FeedData.GetFeedItems(_feed);
                DataContext = mainPage.FeedData;
            }
            catch (Exception)
            {
                MessageBox.Show("Error!! Invalid Link (Uri)");
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }


        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string proChanged)
        {
            if (!string.IsNullOrEmpty(proChanged))
            {
                PropertyChanged(null, new PropertyChangedEventArgs(proChanged));
            }
        }

        #endregion
    }
}